package org.ligson.cg.orm.utils;

import org.ligson.cg.orm.bean.TableInfo;
import org.ligson.cg.orm.config.OrmConfig;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ligson on 2017/8/15.
 */
public class ControllerTemplate {
    private OrmConfig ormConfig;

    public ControllerTemplate(OrmConfig ormConfig) {
        this.ormConfig = ormConfig;
    }

    public OrmConfig getOrmConfig() {
        return ormConfig;
    }

    public void setOrmConfig(OrmConfig ormConfig) {
        this.ormConfig = ormConfig;
    }

    public void write(TableInfo tableInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("tableInfo", tableInfo);
        map.put("entityPackage", ormConfig.getEntityPackage());
        map.put("entityName", ormConfig.getEntityName());
        map.put("baseName", ormConfig.getBaseName());
        map.put("serviceName", ormConfig.getServiceName());
        map.put("servicePackage", ormConfig.getServicePackage());
        map.put("controllerPackage", ormConfig.getControllerPackage());
        map.put("controllerName", ormConfig.getControllerName());
        File controllerDir = new File(ormConfig.getControllerPath());
        if (!controllerDir.exists()) {
            controllerDir.mkdirs();
        }
        File dest = new File(controllerDir, ormConfig
                .getControllerName() + ".java");
        TemplateEngine.write("db/Controller.ftl", map, dest);
    }
}
